package com.example.s14_jpa2.controller;

import com.example.s14_jpa2.pojo.User;
import com.example.s14_jpa2.pojo.UserParam;
import com.example.s14_jpa2.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Controller
public class UserController
{
    @Autowired
    private IUserService userService;

    @RequestMapping("/list")
    public String list(@RequestParam(value = "page",defaultValue = "1") int page,
                       @RequestParam(value = "size",defaultValue = "5") int size,
                       Model m)

    {
        Pageable p = PageRequest.of(page-1,size);
        Page<User>  result= userService.list(p);
        m.addAttribute("page",result);
        System.out.println(result);
        System.out.println(result.toList());
        System.out.println("总页数:"+result.getTotalPages());
        System.out.println(result.isFirst());
        System.out.println(result.isLast());
        System.out.println(result.getNumber());
        System.out.println(result.getPageable().getPageNumber());


        return "list";
    }


    @RequestMapping("/add")
    public String add()
    {
        return "add";
    }

    @RequestMapping("/save")
    public String save(@Valid UserParam userParam, BindingResult result, Model model) {
        String errorMsg="";
        // 参数校验
        if(result.hasErrors()) {
            List<ObjectError> list = result.getAllErrors();
            for (ObjectError error : list) {
                errorMsg=errorMsg + error.getCode() + "-" + error.getDefaultMessage() +";";
            }
            model.addAttribute("errorMsg",errorMsg);
            return "add";
        }
        //判断是否重复添加
        User u= userService.findByUsername(userParam.getUsername());
        if(u!=null){
            model.addAttribute("errorMsg","用户已存在!");
            return "add";
        }
        User user=new User();
        BeanUtils.copyProperties(userParam,user);
        user.setRegTime(new Date());
        //保存
        userService.save(user);
        return "redirect:/list";


    }


    @RequestMapping("/toEdit")
    public String toEdit(int id,Model m)
    {
        User user = userService.findById(id);
        m.addAttribute("user",user);
        return "userEdit";
    }

    @RequestMapping("/edit")
    public String edit(@Valid UserParam userParam, BindingResult result,Model model) {
        String errorMsg="";
        //参数校验
        if(result.hasErrors()) {
            List<ObjectError> list = result.getAllErrors();
            for (ObjectError error : list) {
                errorMsg=errorMsg + error.getCode() + "-" + error.getDefaultMessage() +";";
            }
            model.addAttribute("errorMsg",errorMsg);
            model.addAttribute("user", userParam);
            return "userEdit";
        }

        //复制属性保持修改后数据
        User user=new User();
        BeanUtils.copyProperties(userParam,user);
        user.setRegTime(new Date());
        userService.save(user);
        return "redirect:/list";
    }


    @RequestMapping("/delete")
    public String delete(Long id) {
        userService.deleteById(id);
        return "redirect:/list";
    }
}
